package com.siyoumi.app.modules.app_ess.vo;

import com.siyoumi.util.XJson;
import com.siyoumi.util.XStr;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//测评答案人数
@Data
@Accessors(chain = true)
public class VoEssTestQuestionAnswerTotal {
    String question_id;
    Map<Integer, Integer> items;

    static public VoEssTestQuestionAnswerTotal of(String questionId) {
        VoEssTestQuestionAnswerTotal vo = new VoEssTestQuestionAnswerTotal();
        vo.setQuestion_id(questionId);
        vo.setItems(new HashMap<>());
        return vo;
    }

    /**
     * 加答案人数
     * <p>
     * [0,1] 4
     * 转
     * 0：4
     * 1：4
     *
     * @param answer
     * @param total
     */
    public void addItem(String answer, Integer total) {
        List<Integer> answerArr = new ArrayList<>();
        if (XStr.hasAnyText(answer)) {
            answerArr = XJson.parseArray(answer, Integer.class);
        }

        Map<Integer, Integer> mapAnswer = getItems();
        for (Integer answerItem : answerArr) {
            int answerCount = 0;
            if (mapAnswer.containsKey(answerItem)) {
                answerCount = mapAnswer.get(answerItem);
            }
            //追加数量
            answerCount += total;
            mapAnswer.put(answerItem, answerCount);
        }
    }
}
